package com.keiou.drink.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.keiou.drink.AppContext;
import com.keiou.drink.R;
import com.keiou.drink.api.AbsSubscriber;
import com.keiou.drink.api.exception.ApiException;
import com.keiou.drink.api.remote.Api;
import com.keiou.drink.api.rx.RxHelper;
import com.keiou.drink.glide.GlideUtil;
import com.keiou.drink.response.OrderStatusRes;
import com.keiou.drink.response.base.HttpResult;
import com.keiou.drink.ui.activity.base.RxActivity;
import com.keiou.drink.util.CToast;
import com.keiou.drink.util.QrCodeUtils;
import com.keiou.drink.util.TDevice;
import com.keiou.drink.util.TLog;
import com.keiou.drink.vo.OrderDetail;

import butterknife.BindView;

/**
 * Created by Administrator on 2017/10/29 0029.
 */

public class PayActivity extends RxActivity {

    @BindView(R.id.iv_zfb)
    ImageView ivZfb;
    @BindView(R.id.iv_wx)
    ImageView ivWx;
    @BindView(R.id.iv_product)
    ImageView ivProduct;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_attr)
    TextView tvAttr;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    private OrderDetail orderDetail;

    private Handler handler = new Handler();
    public static final int DELAY = 3000;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pay;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        GlideUtil.loadImagefitCrop(this, orderDetail.getPicUrl(), ivProduct);
    }

    @Override
    protected void initData() {
        orderDetail = (OrderDetail) getIntent().getExtras().get("orderDetail");


        String attr = orderDetail.getAttr();
        if (TextUtils.isEmpty(attr)) {
            tvName.setText(orderDetail.getName() + " x" + orderDetail.getCount());
            tvAttr.setVisibility(View.GONE);
        } else {
            tvName.setText(orderDetail.getName() + "");
            tvAttr.setVisibility(View.VISIBLE);
        }

        if ("1".equals(attr)) {
            tvAttr.setText("无糖" + " x" + orderDetail.getCount());
        } else if ("2".equals(attr)) {
            tvAttr.setText("半糖" + " x" + orderDetail.getCount());
        } else if ("3".equals(attr)) {
            tvAttr.setText("全糖" + " x" + orderDetail.getCount());
        }

        tvPrice.setText(orderDetail.getAmount() + "");

        loadQCode(ivZfb, orderDetail.getAlipayQr());
        loadQCode(ivWx, orderDetail.getWechatPayQr());
    }

    private void loadQCode(ImageView iv, String url) {
        try {
            Bitmap bitmap = QrCodeUtils.Create2DCode(this, url == null ? "empty" : url);
            iv.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void loadData() {

        if (isFinishing()) {
            return;
        }

        handler.postDelayed(() -> loadStatus(), DELAY);

    }

    private void loadStatus() {

        if (isFinishing()) {
            return;
        }


        addSubscribe(Api.getInstance().getOrderStatus(TDevice.getDeviceID(this), orderDetail.getOrderId())
                .compose(RxHelper.applySchedulers())
                .subscribe(new AbsSubscriber<HttpResult<OrderStatusRes>>() {
                    @Override
                    public void onNext(HttpResult<OrderStatusRes> result) {
                        OrderStatusRes orderMsgRes = result.getResult();
                        String status = orderMsgRes.getStatus();
                        if ("0".equals(status)) {
                            loadData();
                        } else if ("1".equals(status) || "2".equals(status)) {
                            AppContext.getInstance().playMedia(1);
                            redirectToResult();
                        } else {
                            CToast.showShort(PayActivity.this, "订单状态出错");
                        }
                    }


                    @Override
                    protected void onError(ApiException ex) {
                        loadData();
                    }

                    @Override
                    protected void onIntercept(int code) {
                        onInterceptCode(code);
                    }
                }));
    }

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    private void redirectToResult() {

        Intent intent = new Intent(this, ResultActivity.class);
        intent.putExtra("orderId", orderDetail.getOrderId());
        startActivity(intent);
        finish();

    }
}
